package SpringMVC.cotizacion;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import SpringMVC.cliente.ClienteRepository;
import SpringMVC.nota.Nota;
import SpringMVC.nota.NotaRepository;

@Controller
@RequestMapping(value = "/cotizacion")
public class CotizacionController {

	@Autowired
	private CotizacionRepository cRepository;

	@Autowired
	private NotaRepository nRepository;

	@Autowired
	private ClienteRepository cliRepository;

	@RequestMapping(value = "/listar")
	public void listar(ModelMap modelMap, @RequestParam(value = "id") Long id) {
		System.out.println("Listar Cotizaciones");
		List<Cotizacion> cotizaciones = cliRepository.getCotizaciones(id);
		modelMap.addAttribute("listaCotizaciones", cotizaciones);
		modelMap.addAttribute("clienteId", id);
		System.out.println("Paso");
	}

	@RequestMapping(value = "/crear")
	public CotizacionForm crear() {
		return new CotizacionForm();
	}

	@RequestMapping(value = "/crear", method = RequestMethod.POST)
	public String crear(@ModelAttribute CotizacionForm cotizacionForm,
			@RequestParam(value = "id") Long id, ModelMap modelMap) {
		
		cRepository.save(cotizacionForm.createCotizacion(id));
		
		modelMap.addAttribute("clienteId", id);

		return "redirect:/cotizacion/listar";
	}

}
